//
//  LoginViewController.m
//  e律师
//
//  Created by 韦淋 on 2017/5/15.
//  Copyright © 2017年 test. All rights reserved.
//

#import "LoginViewController.h"



@interface LoginViewController ()
@property (nonatomic,strong)LoginWarningView *warnView;
@property (nonatomic, strong) UIImageView *backGroundImageView;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UIView *accountView;
@property (nonatomic, strong) UIView *passWordView;
@property (nonatomic, strong) UIImageView *accountImage;
@property (nonatomic, strong) UIImageView *passWordImage;
@property (nonatomic, strong) UITextField *accountField;
@property (nonatomic, strong) UITextField *passWordField;
@property (nonatomic, strong) UIButton *loginButten;
@property (nonatomic, strong) UILabel *infoLable;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self InitializeInterface];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)InitializeInterface{
    [self.view addSubview:self.backGroundImageView];
    [self.view addSubview:self.logoImageView];
    
    [self.view addSubview:self.loginButten];
    [self.view addSubview:self.accountView];
    [self.view addSubview:self.passWordView];
    [self.accountView addSubview:self.accountImage];
    [self.passWordView addSubview:self.passWordImage];
    [self.accountView addSubview:self.accountField];
    [self.passWordView addSubview:self.passWordField];
    [self.view addSubview:self.infoLable];
}
#pragma mark - ButtonTarget
- (void)Login{
    if (self.accountField.text.length == 0) {
        [MBProgressHUD showError:@"请输入账号先"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }else if (self.passWordField.text.length == 0){
        [MBProgressHUD showError:@"请输入密码先"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }else{
//        进行网络请求,,,成功之后就调下面的 block
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSLog(@"登录 ");
        [NetworkingManager getUrl:LOGIN_URL dic:@{@"username":self.accountField.text,@"password":self.passWordField.text} Succeed:^(id responseData) {
            NSLog(@"------------%@",responseData);
            
            [InfoSingleTon personInfoSingleTon].infoDic = [NSMutableDictionary dictionaryWithDictionary:responseData];
            if ([responseData[@"status"] integerValue] == 100) {
                [MBProgressHUD showError:@"账户密码错误，登录失败"];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                //登录失败
                            [_warnView wrongWarningWithString:@"账户密码错误，登录失败"];
                
            }else{
                            [_warnView wrongWarningWithString:@"登录成功"];
                //存
                [[NSUserDefaults standardUserDefaults]setObject:self.accountField.text forKey:@"accountNumber"];
                [[NSUserDefaults standardUserDefaults]setObject:responseData forKey:@"userMessage"];
                
                [MBProgressHUD showError:@"登录成功"];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if (_LogOut == 1) {// 点击退出登录;登录成功后调用
                    [self dismissViewControllerAnimated:NO completion:nil];
                }
                self.success ? self.success() : nil;
                
            }
            
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络错误"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
   
   
}

#pragma mark - Getter/Setter
- (LoginWarningView *)warnView{
    if (!_warnView) {
        _warnView = [[LoginWarningView alloc]init];
    }
    return _warnView;
}
- (UIImageView *)backGroundImageView{
    if (!_backGroundImageView) {
        _backGroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1top"]];
        _backGroundImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 350*Hbi);
    }
    return _backGroundImageView;
}
- (UIImageView *)logoImageView{
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init]; //WithFrame:CGRectMake(110, 80, 160, 140)];
        _logoImageView.center = self.backGroundImageView.center;
        _logoImageView.bounds = CGRectMake(0, 0, 160*Wbi, 140*Hbi);
        _logoImageView.contentMode = UIViewContentModeScaleAspectFit;
        _logoImageView.image = [UIImage imageNamed:@"logo.jpg"];
    }
    return _logoImageView;
}
- (UIView *)accountView{
    if (!_accountView) {
        _accountView = [[UIView alloc] initWithFrame:CGRectMake(50*Wbi, 350*Hbi, SCREEN_WIDTH - 100, 40*Hbi)];
        _accountView.layer.masksToBounds = YES;
        _accountView.layer.cornerRadius = 20;
        _accountView.layer.borderWidth = 1;
        _accountView.layer.borderColor = [UIColor colorWithRed:225.0f/255.0f green:225.0f/255.0f blue:225.0f/255.0f alpha:1.0f].CGColor;
    }
    return _accountView;
}
- (UIView *)passWordView{
    if (!_passWordView) {
        _passWordView = [[UIView alloc] initWithFrame:CGRectMake(50*Wbi, 400*Hbi, SCREEN_WIDTH - 100, 40*Hbi)];
        _passWordView.layer.masksToBounds = YES;
        _passWordView.layer.cornerRadius = 20;
        _passWordView.layer.borderWidth = 1;
        _passWordView.layer.borderColor = [UIColor colorWithRed:225.0f/255.0f green:225.0f/255.0f blue:225.0f/255.0f alpha:1.0f].CGColor;
    }
    return _passWordView;
}
- (UIImageView *)accountImage{
    if (!_accountImage) {
        _accountImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1zhang.jpg"]];
        _accountImage.frame = CGRectMake(20, 10, 20, 20);
    }
    return _accountImage;
}
- (UIImageView *)passWordImage{
    if (!_passWordImage) {
        _passWordImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1mim.jpg"]];
        _passWordImage.frame = CGRectMake(20, 10, 20, 20);
    }
    return _passWordImage;
}
- (UITextField *)accountField{
    if (!_accountField) {
        _accountField = [[UITextField alloc] initWithFrame:CGRectMake(50*Wbi, 5, SCREEN_WIDTH - 180, 30)];
        _accountField.placeholder = @"用户名 ";
        _accountField.font = [UIFont systemFontOfSize:15];
        _accountField.returnKeyType = UIReturnKeyNext;//设置按键类型
        _accountField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _accountField.enablesReturnKeyAutomatically = YES;//无文字就灰色不可点
    }
    return _accountField;
}

- (UITextField *)passWordField{
    if (!_passWordField) {
        _passWordField = [[UITextField alloc] initWithFrame:CGRectMake(50*Wbi, 5, SCREEN_WIDTH - 180, 30)];
        _passWordField.placeholder = @"密码 ";
        _passWordField.font = [UIFont systemFontOfSize:15];
        _passWordField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passWordField.returnKeyType = UIReturnKeyGo;//设置按键类型
        _passWordField.secureTextEntry = YES;
        _passWordField.enablesReturnKeyAutomatically = YES;//无文字就灰色不可点
    }
    return _passWordField;
}
- (UIButton *)loginButten{
    if (!_loginButten) {
        _loginButten = [[UIButton alloc] initWithFrame:CGRectMake(50*Wbi, 465*Hbi, SCREEN_WIDTH - 100, 40)];
        _loginButten.backgroundColor = [UIColor colorWithRed:103.0f/255.0f green:176.0f/255.0f blue:252.0f/255.0f alpha:1.0f];
        _loginButten.layer.masksToBounds = YES;
        _loginButten.layer.cornerRadius = 20;
        [_loginButten setTitle:@"登录" forState:0];
        [_loginButten setTitleColor:[UIColor whiteColor] forState:0];
        [_loginButten addTarget:self action:@selector(Login) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButten;
}
- (UILabel *)infoLable{
    if (!_infoLable) {
        _infoLable = [[UILabel alloc] initWithFrame:CGRectMake(50*Wbi, 600*Hbi, SCREEN_WIDTH - 100, 50*Hbi)];
        _infoLable.textColor = [UIColor lightGrayColor];
        _infoLable.font = [UIFont systemFontOfSize:13];
        _infoLable.numberOfLines = 2;
        _infoLable.textAlignment = NSTextAlignmentCenter;
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Copyright © yunlvsi.cn 2007-7017 In All Rights Reserved"];
        
        //修改颜色
        [string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(10, 12)];
        
        _infoLable.attributedText = string;
        
        
//        _infoLable.text = @"Copyright © yunlvsi.cn 2007-7017 In All Rights Reserved";
        
    }
    return _infoLable;
}

- (void)dealloc{
    NSLog(@"LoginView dealloc");
}

@end
